//
//  MOPickerView.m
//  WCImApp
//
//  Created by WCImApp on 2019/4/12.
//  Copyright © 2019 WCImApp. All rights reserved.
//

#import "MOPickerView.h"
#import "FWPopupBaseView.h"
@interface MOPickerView () <UIPickerViewDelegate,UIPickerViewDataSource>

@property (nonatomic, strong) FWPopupBaseView *customView;
@property (nonatomic, strong) UIButton *cancelBtn;
@property (nonatomic, strong) UIButton *sureBtn;
@property (nonatomic, strong) UILabel *titleLab;

@end

@implementation MOPickerView

- (instancetype)init {
    self = [super init];
    if (self) {
        self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        [self setupView];
    }
    
    return self;
}

#pragma mark - Private Methods

- (void)setupView {

    _customView = [[FWPopupBaseView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 360 + WCI_IPHONE_SAFEBOTTOMAREA_HEIGHT)];
    FWPopupBaseViewProperty *property = [FWPopupBaseViewProperty manager];
    property.popupAlignment = FWPopupAlignmentBottomCenter;
    property.popupAnimationStyle = FWPopupAnimationStyleFrame;
    property.maskViewColor = [UIColor colorWithLightColor:[[UIColor blackColor] colorWithAlphaComponent:0.4f] DarkColor:[[UIColor blackColor] colorWithAlphaComponent:0.6f]];
    property.touchWildToHide = @"0";
    property.popupEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    property.animationDuration = 0.2;
    _customView.vProperty = property;
    _customView.backgroundColor = assist_WB_color;

    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:_customView.bounds byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight) cornerRadii:CGSizeMake(16,16)];

    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = _customView.bounds;
    maskLayer.path = maskPath.CGPath;
    _customView.layer.mask = maskLayer;

    
    CGFloat topH = 56;

//    UIView *pickBck = [[UIView alloc] init];
//    pickBck.backgroundColor = assist_viewBg_color;
//    [self.customView addSubview:pickBck];
//    ViewRadius(pickBck, 8);
//    [pickBck mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(self.customView.mas_left).offset(15);
//        make.right.equalTo(self.customView.mas_right).offset(-15);
//        make.top.equalTo(self.customView.mas_top).offset(topH);
//        make.height.mas_equalTo(200);
//    }];
    

    //选择器
    self.pickView = [[UIPickerView alloc] init];
    self.pickView.backgroundColor = [UIColor clearColor];
    self.pickView.dimMaskViewColor = [UIColor clearColor];
    self.pickView.tintColor = [UIColor clearColor];
    self.pickView.delegate = self;
    self.pickView.dataSource = self;
    ViewRadius(self.pickView, 5);
    [self.customView addSubview:self.pickView];
    
    [self.pickView setValue:[UIColor main_Text_color] forKey:@"textColor"];
        if ([self.pickView respondsToSelector:sel_registerName("setHighlightsToday:")]) {
    #pragma clang diagnostic push
    #pragma clang diagnostic ignored "-Wundeclared-selector"
            [self.pickView performSelector:@selector(setHighlightsToday:) withObject:[NSNumber numberWithBool:NO]];
    #pragma clang diagnostic pop
        }
    
    [self.pickView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.customView.mas_left).offset(15);
        make.right.equalTo(self.customView.mas_right).offset(-15);
        make.top.equalTo(self.customView.mas_top).offset(70);
        make.height.mas_equalTo(200);
    }];
    
//    UIView *white = [[UIView alloc] init];
//    white.backgroundColor = [UIColor colorWithLightColorStr:@"FFFFFF" DarkColor:@"404040"];
//    [pickBck addSubview:white];
//    [white mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.height.mas_equalTo(40);
//        make.centerY.mas_equalTo(0);
//        make.left.mas_equalTo(0);
//        make.right.mas_equalTo(-1);
//    }];
//
//    UIView *leftItem = [[UIView alloc] init];
//    leftItem.backgroundColor = main_blue_color;
//    [white addSubview:leftItem];
//    [leftItem mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(0);
//        make.bottom.mas_equalTo(0);
//        make.width.mas_equalTo(4);
//        make.left.mas_equalTo(0);
//    }];
    
    //头部
//    UIView *viewTopBG = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _customView.width, topH)];
//    [self.customView addSubview:viewTopBG];
    
    self.cancelBtn = [[UIButton alloc] initWithFrame:CGRectMake(10, 0, 40, topH)];
    [_cancelBtn setTitle:(@"取消") forState:UIControlStateNormal];
    _cancelBtn.tag = 1000;
    _cancelBtn.backgroundColor = [UIColor colorWithLightColorStr:@"#F7F7F8" DarkColor:@"#1E1F22"];
    _cancelBtn.titleLabel.font = WCIFontMedium(16);
    [_cancelBtn setTitleColor:assist_text_color forState:UIControlStateNormal];
    [_cancelBtn addTarget:self action:@selector(cancelAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.customView addSubview:_cancelBtn];
    ViewRadius(_cancelBtn, 8);
    
    self.sureBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 48, 32)];;
    [_sureBtn setTitle:(@"完成") forState:UIControlStateNormal];
    [_sureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _sureBtn.backgroundColor = main_color;
    _sureBtn.titleLabel.font = WCIFontMedium(14);
    _sureBtn.tag = 1001;
    [_sureBtn addTarget:self action:@selector(finishAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.customView addSubview:_sureBtn];
    ViewRadius(_sureBtn, 8);
    
    self.titleLab = [[UILabel alloc] initWithFrame:CGRectMake(topH, 0, _customView.width -100, topH)];
    _titleLab.textAlignment = NSTextAlignmentCenter;
    _titleLab.textColor = assist_text_color;
    _titleLab.font = WCIFontRegular(14);
    [self.customView addSubview:_titleLab];
    
    UIView *line = [[UIView alloc] init];
    line.backgroundColor = assist_line_color;
    [self.customView addSubview:line];
    
    [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(32);
        make.right.offset(-32);
        make.height.mas_equalTo(50);
        make.bottom.offset(-40);
    }];
    
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(32);
        make.top.offset(26);
    }];
    
    [self.sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-32);
        make.top.offset(20);
        make.size.mas_equalTo(CGSizeMake(48, 32));
    }];
    
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(0.5);
        make.left.offset(32);
        make.right.offset(-32);
        make.bottom.equalTo(self.sureBtn.mas_top).offset(-29);
    }];
    
}

- (void)show {
    [_customView show];
}

- (void)hide {
    [_customView hide];
}

#pragma mark - Click Event

- (void)cancelAction:(UIButton *)sender {
    if (self.callBackToCancel) {
        self.callBackToCancel();
    }
    
    [self hide];
}

- (void)finishAction:(UIButton *)sender {
    if (self.callBackToFinish) {
        self.callBackToFinish(_selectRow);
    }
    
    [self hide];
}

#pragma mark - UIPickerViewDelegate && UIPickerViewDataSource

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return self.pickData.count;
}

- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    return self.pickData[row];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    NSLog(@"pick view row === %ld  component == %ld",(long)row,(long)component);
    _selectRow = row;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return 30;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
//    ((UIView *)[pickerView.subviews objectAtIndex:1]).hidden = YES;
//    ((UIView *)[pickerView.subviews objectAtIndex:2]).hidden = YES;
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, SCREEN_WIDTH - 30, 50)];
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor main_Text_color];
    label.text = self.pickData[row];
    
    return label;
}

#pragma mark - Set && Get

- (void)setPickData:(NSMutableArray *)pickData {
    _pickData = pickData;
    [_pickView reloadAllComponents];
}

- (void)setTitle:(NSString *)title {
    _title = title;
    _titleLab.text = title;
}

- (void)setSureButtonTitle:(NSString *)sureButtonTitle {
    _sureButtonTitle = sureButtonTitle;
    [_sureBtn setTitle:sureButtonTitle forState:UIControlStateNormal];
}

@end
